package com.taotao.manage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.JedisService.JedisService;
import com.taotao.common.bean.ItemCatData1;
import com.taotao.common.bean.ItemCatData2;
import com.taotao.common.bean.ItemCatResult;
import com.taotao.manage.mapper.ItemCatMapper;
import com.taotao.manage.pojo.ItemCat;

@Service
public class ItemCatService extends BaseService<ItemCat>{
	
	@Autowired
	private ItemCatMapper itemCatMapper;
	
	@Autowired
	public JedisService jedisService;

	public ItemCatResult queryAllItemCat() {
		// 先从缓存中获取数据
		ItemCatResult itemCatResult = this.getItemCatResultByRedis();
		if(itemCatResult != null) return itemCatResult;
		// 缓存中没有, 查询数据库
		ItemCatResult result = new ItemCatResult();
		List<ItemCatData1> itemDatas = new ArrayList<ItemCatData1>();
		result.setData(itemDatas);
		//一次查出所有的商品类目数据
		List<ItemCat> itemcatAll = super.queryAll();
		// Map<父Id,子集合>
		Map<Long,List<ItemCat>> map = new HashMap<Long,List<ItemCat>>();
		for(ItemCat ic : itemcatAll){
			Long parentId = ic.getParentId();
			// 如果parentId在map的key中不存在
			if(!map.containsKey(parentId)){
				map.put(parentId, new ArrayList<ItemCat>());
			}
			List<ItemCat> list = map.get(parentId);
			list.add(ic);
		}
		//最顶级类目
		List<ItemCat> itemcatList1 = map.get(0L);
		for(ItemCat itemcat : itemcatList1){
				ItemCatData1 icd1 = new ItemCatData1();
				icd1.setUrl("/products/" + itemcat.getId() + ".html");
				icd1.setName("<a href='/products/" + itemcat.getId() + ".html'>" + itemcat.getName() + "</a>");
				List<ItemCatData2> itemcatData2List = new ArrayList<ItemCatData2>();
				icd1.setI(itemcatData2List);
				itemDatas.add(icd1);
				//获取当前类目的所有子类目  二级类目
				List<ItemCat> itemcatList2 = map.get(itemcat.getId());
				for(ItemCat itemcat2 : itemcatList2){
					ItemCatData2 itemcatData2 = new ItemCatData2();
					itemcatData2.setUrl("/products/" + itemcat.getId() + ".html");
					itemcatData2.setName(itemcat.getName());
					List<String> listStr = new ArrayList<String>();
					itemcatData2.setI(listStr);
					itemcatData2List.add(itemcatData2 );
					//获取当前类目的子类目   三级类目
					List<ItemCat> itemcatList3 = map.get(itemcat2.getId());
					for(ItemCat itemcat3 : itemcatList3){
						listStr.add("/products/"+itemcat3.getId()+".html|" + itemcat3.getName());
					}
				}
				if(result.getData().size() >= 14){
					break;
				}
			}
		try {
			// 将数据存入缓存
			String itemCatResultJson = PropertieService.MAPPER.writeValueAsString(result);
			jedisService.setExpr(propertieService.TAOTAO_MANAGE_JEDISCACHED_ITEMCAT,propertieService.TAOTAO_MANAGE_JEDISCACHED_ITEMCAT_LIVE,itemCatResultJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	// 从缓存中查询ItemCatResult
	public ItemCatResult getItemCatResultByRedis(){
		try {
			// 先从缓存中查询
			String itemCatResultJson = jedisService.get(propertieService.TAOTAO_MANAGE_JEDISCACHED_ITEMCAT);
			if(StringUtils.isNotBlank(itemCatResultJson)){
				// 缓存中找到后直接返回
				return PropertieService.MAPPER.readValue(itemCatResultJson, ItemCatResult.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}












